package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Guru;
import edu.unindra.raport.dao.impl.GuruImpl;

import javax.swing.JComboBox;

import com.toedter.calendar.JDateChooser;

import java.awt.Font;
import java.awt.Color;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JTextArea;

public class FormDetailMenuGuruAdd extends JDialog {

	private static final long serialVersionUID = 1L;

	private FormDetailMenuGuru parent;

	private JPanel jContentPane = null;
	private JButton btnTambah = null;
	private JLabel lblNip;

	private JTextField txtNip;
	private JButton btnReset;
	private JButton btnBatal;
	private JLabel lblTeLahir;
	private JLabel lblJbtn;
	private JTextField txtTeLahir;

	@SuppressWarnings("unused")
	private String invalidMessage = "";

	private Guru si = null;
	private GuruImpl siImpl = null;

	private JLabel lblNamaGuru;
	private JTextField txtNama;
	private JLabel lblTaLahir;
	@SuppressWarnings("rawtypes")
	private JComboBox cbJbtn;
	private JLabel lblPndkn;
	@SuppressWarnings("rawtypes")
	private JComboBox cbPndkn;
	private JDateChooser dateChooser;

	private DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
	private JLabel lblBidStudy;
	@SuppressWarnings("rawtypes")
	private JComboBox cbBgdStudy;
	private JLabel lblAlamat;
	private JTextArea textArea;

	public FormDetailMenuGuruAdd(FormDetailMenuGuru parent) {
		this.parent = parent;

		siImpl = new GuruImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Tambah Data Guru");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 334);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNip = new GridBagConstraints();
			gbc_lblNip.gridx = 1;
			gbc_lblNip.anchor = GridBagConstraints.WEST;
			gbc_lblNip.insets = new Insets(2, 2, 5, 5);
			gbc_lblNip.weightx = 0.0D;
			gbc_lblNip.gridy = 0;
			this.lblNip = new JLabel();
			lblNip.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNip.setText("Nip");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0,
					0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0, 0.0, 1.0, 0.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNip = new GridBagConstraints();
			gbc_txtNip.gridwidth = 3;
			gbc_txtNip.insets = new Insets(0, 0, 5, 5);
			gbc_txtNip.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNip.gridx = 3;
			gbc_txtNip.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNip);
			this.jContentPane.add(this.lblNip, gbc_lblNip);
			GridBagConstraints gbc_lblNamaGuru = new GridBagConstraints();
			gbc_lblNamaGuru.anchor = GridBagConstraints.WEST;
			gbc_lblNamaGuru.insets = new Insets(0, 0, 5, 5);
			gbc_lblNamaGuru.gridx = 1;
			gbc_lblNamaGuru.gridy = 1;
			jContentPane.add(getLblNamaGuru(), gbc_lblNamaGuru);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 1;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblTeLahir = new GridBagConstraints();
			gbc_lblTeLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTeLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTeLahir.gridx = 1;
			gbc_lblTeLahir.gridy = 2;
			jContentPane.add(getLblTeLahir(), gbc_lblTeLahir);
			GridBagConstraints gbc_txtTeLahir = new GridBagConstraints();
			gbc_txtTeLahir.gridwidth = 3;
			gbc_txtTeLahir.insets = new Insets(0, 0, 5, 5);
			gbc_txtTeLahir.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTeLahir.gridx = 3;
			gbc_txtTeLahir.gridy = 2;
			jContentPane.add(getTxtTeLahir(), gbc_txtTeLahir);
			GridBagConstraints gbc_lblTaLahir = new GridBagConstraints();
			gbc_lblTaLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTaLahir.gridx = 1;
			gbc_lblTaLahir.gridy = 3;
			jContentPane.add(getLblTaLahir(), gbc_lblTaLahir);
			GridBagConstraints gbc_dateChooser = new GridBagConstraints();
			gbc_dateChooser.gridwidth = 3;
			gbc_dateChooser.insets = new Insets(0, 0, 5, 5);
			gbc_dateChooser.fill = GridBagConstraints.HORIZONTAL;
			gbc_dateChooser.gridx = 3;
			gbc_dateChooser.gridy = 3;
			jContentPane.add(getDateChooser(), gbc_dateChooser);
			GridBagConstraints gbc_lblJbtn = new GridBagConstraints();
			gbc_lblJbtn.anchor = GridBagConstraints.WEST;
			gbc_lblJbtn.insets = new Insets(0, 0, 5, 5);
			gbc_lblJbtn.gridx = 1;
			gbc_lblJbtn.gridy = 4;
			jContentPane.add(getLblJbtn(), gbc_lblJbtn);
			GridBagConstraints gbc_cbJbtn = new GridBagConstraints();
			gbc_cbJbtn.gridwidth = 3;
			gbc_cbJbtn.insets = new Insets(0, 0, 5, 5);
			gbc_cbJbtn.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbJbtn.gridx = 3;
			gbc_cbJbtn.gridy = 4;
			jContentPane.add(getCbJbtn(), gbc_cbJbtn);
			GridBagConstraints gbc_lblPndkn = new GridBagConstraints();
			gbc_lblPndkn.anchor = GridBagConstraints.WEST;
			gbc_lblPndkn.insets = new Insets(0, 0, 5, 5);
			gbc_lblPndkn.gridx = 1;
			gbc_lblPndkn.gridy = 5;
			jContentPane.add(getLblPndkn(), gbc_lblPndkn);
			GridBagConstraints gbc_cbPndkn = new GridBagConstraints();
			gbc_cbPndkn.gridwidth = 3;
			gbc_cbPndkn.insets = new Insets(0, 0, 5, 5);
			gbc_cbPndkn.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbPndkn.gridx = 3;
			gbc_cbPndkn.gridy = 5;
			jContentPane.add(getCbPndkn(), gbc_cbPndkn);
			GridBagConstraints gbc_lblBidStudy = new GridBagConstraints();
			gbc_lblBidStudy.insets = new Insets(0, 0, 5, 5);
			gbc_lblBidStudy.gridx = 1;
			gbc_lblBidStudy.gridy = 6;
			jContentPane.add(getLblBidStudy(), gbc_lblBidStudy);
			GridBagConstraints gbc_cbBgdStudy = new GridBagConstraints();
			gbc_cbBgdStudy.gridwidth = 3;
			gbc_cbBgdStudy.insets = new Insets(0, 0, 5, 5);
			gbc_cbBgdStudy.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbBgdStudy.gridx = 3;
			gbc_cbBgdStudy.gridy = 6;
			jContentPane.add(getCbBgdStudy(), gbc_cbBgdStudy);
			GridBagConstraints gbc_lblAlamat = new GridBagConstraints();
			gbc_lblAlamat.anchor = GridBagConstraints.WEST;
			gbc_lblAlamat.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlamat.gridx = 1;
			gbc_lblAlamat.gridy = 7;
			jContentPane.add(getLblAlamat(), gbc_lblAlamat);
			GridBagConstraints gbc_textArea = new GridBagConstraints();
			gbc_textArea.gridheight = 2;
			gbc_textArea.gridwidth = 3;
			gbc_textArea.insets = new Insets(0, 0, 5, 5);
			gbc_textArea.fill = GridBagConstraints.BOTH;
			gbc_textArea.gridx = 3;
			gbc_textArea.gridy = 7;
			jContentPane.add(getTextArea(), gbc_textArea);
			GridBagConstraints gbc_btnReset = new GridBagConstraints();
			gbc_btnReset.insets = new Insets(0, 0, 0, 5);
			gbc_btnReset.gridx = 4;
			gbc_btnReset.gridy = 9;
			jContentPane.add(getBtnReset(), gbc_btnReset);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 9;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnTambah = new GridBagConstraints();
			gbc_btnTambah.gridx = 6;
			gbc_btnTambah.insets = new Insets(2, 2, 2, 0);
			gbc_btnTambah.anchor = 13;
			gbc_btnTambah.gridy = 9;
			this.jContentPane.add(getBtnTambah(), gbc_btnTambah);
		}
		return this.jContentPane;
	}

	private JButton getBtnTambah() {
		if (this.btnTambah == null) {
			this.btnTambah = new JButton();
			this.btnTambah.setText("Tambah");
			this.btnTambah.setToolTipText("Insert");
			this.btnTambah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (isDataSiswaValid()) {
						int retVal = 0;
						si = new Guru();

						si.setNip(txtNip.getText());
						si.setNama(txtNama.getText());
						si.setTeTgl(txtTeLahir.getText());
						si.setTaTgl(dateFormat.format(dateChooser.getDate()));
						si.setJbtn(cbJbtn.getSelectedItem().toString());
						si.setPndkn(cbPndkn.getSelectedItem().toString());
						si.setBidStudy(cbBgdStudy.getSelectedItem().toString());
						si.setAlamat(textArea.getText());

						try {
							retVal = siImpl.insert(si);
						} catch (SQLException e1) {
							e1.printStackTrace();
						}

						if (retVal > -1) {
							JOptionPane.showMessageDialog(
									FormDetailMenuGuruAdd.this,
									"Proses penambahan data guru berhasil");
							parent.getModelTable().addGuru(si);

							FormDetailMenuGuruAdd.this.setVisible(false);
						} else {
							JOptionPane.showMessageDialog(
									FormDetailMenuGuruAdd.this,
									"Proses input data guru problem");
						}
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuGuruAdd.this,
								"Proses penambahan data guru  problem");

					}

				}
			});
		}
		return this.btnTambah;
	}

	JTextField getTxtCardNumber() {
		if (txtNip == null) {
			txtNip = new JTextField();
			txtNip.setColumns(10);
		}
		return txtNip;
	}

	private JButton getBtnReset() {
		if (btnReset == null) {
			btnReset = new JButton();
			btnReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonResetActionPerformed(e);
				}
			});
			btnReset.setToolTipText("Insert");
			btnReset.setText("Reset");
		}
		return btnReset;
	}

	private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {
		reset();
	}

	private void reset() {
		txtNip.setText("");
		txtNama.setText("");
		txtTeLahir.setText("");
		cbJbtn.setSelectedIndex(0);
		cbPndkn.setSelectedIndex(0);

		setFocusable(true);
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonBatalActionPerformed(e);
				}
			});
			btnBatal.setToolTipText("Batal");
			btnBatal.setText("Batal");
		}
		return btnBatal;
	}

	private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {
		FormDetailMenuGuruAdd.this.setVisible(false);
	}

	private JLabel getLblTeLahir() {
		if (lblTeLahir == null) {
			lblTeLahir = new JLabel();
			lblTeLahir.setText("Tempat Lahir");
		}
		return lblTeLahir;
	}

	private JLabel getLblJbtn() {
		if (lblJbtn == null) {
			lblJbtn = new JLabel();
			lblJbtn.setText("Jabatan");
		}
		return lblJbtn;
	}

	private JTextField getTxtTeLahir() {
		if (txtTeLahir == null) {
			txtTeLahir = new JTextField();
			txtTeLahir.setColumns(10);
		}
		return txtTeLahir;
	}

	private boolean isDataSiswaValid() {
		if (txtNip.getText().trim().length() == 0) {
			invalidMessage = "Nis Siswa masih kosong";
			return false;
		}

		if (txtNama.getText().trim().length() == 0) {
			invalidMessage = "Nama Siswa masih kosong";
			return false;
		}

		if (txtTeLahir.getText().trim().length() == 0) {
			invalidMessage = "Tempat Lahir  masih kosong";
			return false;
		}

		return true;
	}

	public Guru getGuru() {
		return si;
	}

	public void setGuru(Guru si) {
		this.si = si;
	}

	private JLabel getLblNamaGuru() {
		if (lblNamaGuru == null) {
			lblNamaGuru = new JLabel();
			lblNamaGuru.setText("Nama Guru");
		}
		return lblNamaGuru;
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	private JLabel getLblTaLahir() {
		if (lblTaLahir == null) {
			lblTaLahir = new JLabel();
			lblTaLahir.setText("Tanggal Lahir");
		}
		return lblTaLahir;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbJbtn() {
		if (cbJbtn == null) {
			cbJbtn = new JComboBox();
			cbJbtn.setModel(new DefaultComboBoxModel(new String[] { "KepSek",
					"WaKepSek", "Guru" }));
		}
		return cbJbtn;
	}

	private JLabel getLblPndkn() {
		if (lblPndkn == null) {
			lblPndkn = new JLabel();
			lblPndkn.setText("Pendidikan");
		}
		return lblPndkn;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbPndkn() {
		if (cbPndkn == null) {
			cbPndkn = new JComboBox();
			cbPndkn.setModel(new DefaultComboBoxModel(new String[] { "SLTA",
					"Diploma", "Strata 1", "Strata 2" }));
		}
		return cbPndkn;
	}

	private JDateChooser getDateChooser() {
		if (dateChooser == null) {
			dateChooser = new JDateChooser();
			dateChooser.getSpinner().setForeground(Color.BLACK);
			dateChooser.setFont(new Font("Tahoma", Font.BOLD, 11));
		}
		return dateChooser;
	}

	private JLabel getLblBidStudy() {
		if (lblBidStudy == null) {
			lblBidStudy = new JLabel();
			lblBidStudy.setText("Bidang Study");
		}
		return lblBidStudy;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbBgdStudy() {
		if (cbBgdStudy == null) {
			cbBgdStudy = new JComboBox();
			cbBgdStudy.setModel(new DefaultComboBoxModel(new String[] {
					"Bahasa Indonesia", "Bahasa Inggris", "Matematika",
					"Ilmu Pengetahuan Alam", "Ilmu Pengetahuan Sosial" }));
		}
		return cbBgdStudy;
	}

	private JLabel getLblAlamat() {
		if (lblAlamat == null) {
			lblAlamat = new JLabel();
			lblAlamat.setText("Alamat");
		}
		return lblAlamat;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}